package com.koala.aideddevsystemmodule.modules.uploadFile.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;

/**
 * SpringBoot上传文件相关配置
 *      参考：https://www.yisu.com/zixun/691775.html
 *
 * Create by koala on 2022-08-22
 */
@Configuration
public class UploadFileConfiguration {
    @Value("${spring.web.upload.maxFileSize:1}")
    private long maxFileSize;

    @Value("${spring.web.upload.maxRequestSize:1}")
    private long maxRequestSize;

    public UploadFileConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean({MultipartConfigElement.class})
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(this.maxFileSize);
        factory.setMaxRequestSize(this.maxRequestSize);
        return factory.createMultipartConfig();
    }
}